package com.example.statistics.ui.market

import android.os.Bundle
import android.widget.ImageView
import com.example.statistics.R
import com.example.statistics.bean.*
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.glide
import com.example.statistics.net.UrlConfig
import com.example.statistics.net.callback.DataDialogHttpCallback
import com.example.statistics.ui.product.ProductInfoEditActivity
import com.example.statistics.utils.Constants
import com.jhj.httplibrary.HttpCall
import com.jhj.imageselector.utils.toArrayList
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.itemdecoration.LineItemDecoration
import com.tencent.mm.opensdk.utils.Log
import kotlinx.android.synthetic.main.activity_recyclerview_refresh.*
import org.jetbrains.anko.dip
import org.jetbrains.anko.leftPadding
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.startActivity
import java.util.ArrayList
import java.util.logging.Logger

class MarketCityTreeActivity : BaseActivity() {

    var action: String? = ""
    var adapter: SlimAdapter? = null

    override val title: String
        get() = "城市"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recyclerview_refresh)
        initAdapter()
        refresh(adapter)
    }

    private fun refresh(adapter: SlimAdapter?) {
        HttpCall.get(UrlConfig.MARKET_CITY_TREE)
            .enqueue(object : DataDialogHttpCallback<List<ProvinceNodeBean>>(this) {
                override fun onSuccess(data: List<ProvinceNodeBean>?, resultType: ResultType) {
                    val tree = format(data.orEmpty())
                     adapter?.setDataList(tree.orEmpty())
                }
            })

        HttpCall.get(UrlConfig.MARKET_CUSTOMER_LIST)
            .enqueue(object : DataDialogHttpCallback<List<MarketCustomerBean>>(this) {
                override fun onSuccess(data: List<MarketCustomerBean>?, resultType: ResultType) {
                    val tree = formatCustomer(data.orEmpty())
                    val b = tree
                    //  adapter?.setDataList(tree.orEmpty())
                }

            })
    }

    private fun initAdapter() {
        adapter = SlimAdapter()
        recyclerView.addItemDecoration(LineItemDecoration())
        adapter?.registerTree<CityTreeBean>(
            R.layout.tree_item_goods_root,
            R.layout.tree_item_goods_node
        ) { injector, bean, position ->
            val imageView = injector.getView<ImageView>(R.id.image_view)
            injector.text(R.id.tv_title, bean.name)
                .with<ImageView>(R.id.iv_arrow) {
                    if (bean.isRoot) {
                        it.glide(R.drawable.ic__eye_black)
                    } else {
                        it.glide(R.drawable.ic_arrow_right_black)
                    }

                    it.onClick {
                        startActivity<MarketCustomerListActivity>(Constants.CITY to bean.name)
                    }
                }
                .with<ImageView>(R.id.image_view) {
                    if (bean.isRoot) {
                        it.glide(R.drawable.ic_tree_node_close)
                    } else {
                        it.glide(R.drawable.ic_tree_node)
                    }
                }
                .with {
                    it.leftPadding = dip(20) * bean.itemLevels
                }
                .clicked {
                    itemClickResponse(bean,
                        itemClose = {
                            imageView.glide(R.drawable.ic_tree_node_close)
                        },
                        itemExpend = {
                            imageView.glide(R.drawable.ic_tree_root_expend)
                        },
                        itemNode = {
                            startActivity<MarketCustomerListActivity>(Constants.ID to bean.id)
                        })
                }
        }
            ?.attachTo(recyclerView)
    }

    fun formatCustomer(list: List<MarketCustomerBean>): List<CityTreeBean> {
        val tree = mutableListOf<CityTreeBean>()
        list.forEach { customer ->
            val market = CityTreeBean(customer.marketId, customer.market, 3, false)

            if (tree.none { it.name == customer.province }) {  //树中不包含省份
                Log.e("省份",customer.province)
                val city = CityTreeBean(customer.city, 2, true, arrayListOf(market))
                val province = CityTreeBean(customer.province, 1, true, arrayListOf(city))
                tree.add(province)
            } else {  //树中包含省份
                val province = tree.find { it.name == customer.province }
                val cityList = province?.getChildren().orEmpty().toArrayList()
                if (cityList.none { it.name == customer.city }) { //省份中不包含该城市
                    val city = CityTreeBean(customer.city, 2, true,arrayListOf(market))
                    cityList.add(city)
                    province?.setChildren(cityList)
                } else {//省份中包含该城市
                    val city = cityList.find { it.name == customer.city }
                    val marketList = city?.getChildren().orEmpty().toArrayList()
                    if (marketList.none { it.name == customer.market }) {//城市不包含该市场
                        marketList.add(market)
                        city?.setChildren(marketList)
                    }else{

                    }
                }
            }
        }
        return tree
    }

    fun format(list: List<ProvinceNodeBean>): List<CityTreeBean> {
        val tree = mutableListOf<CityTreeBean>()
        list.forEach { iProvince ->
            val province = CityTreeBean()
            val cityList = arrayListOf<CityTreeBean>()
            province.name = iProvince.province
            province.levels = 1
            province.isRoot = true
            iProvince.cityList.forEach { iCity ->
                val city = CityTreeBean()
                val marketList = arrayListOf<CityTreeBean>()
                city.name = iCity.city
                city.levels = 2
                city.isRoot = true
                iCity.marketList.forEach {
                    val market = CityTreeBean()
                    market.name = it.market
                    market.id = it.marketId
                    market.levels = 3
                    market.isRoot = false
                    marketList.add(market)
                }
                city.setChildren(marketList)
                cityList.add(city)
            }
            province.setChildren(cityList)
            tree.add(province)
        }
        return tree
    }
}
